package com.gaoyang.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gaoyang.util.HttpUtils4Black;
import com.gaoyang.util.MailUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class JingYiTongTask {

//    @Scheduled(fixedRate = 5000)
    public String start() {
        StringBuffer sb = new StringBuffer();
        String hosCode = "H107861";
//        String hisId = initParams.get("hisId");
//        String wxId = initParams.get("wxId");
        Map<String, String> params = new HashMap<>();
        params.put("hosCode", hosCode);
        String result = HttpUtils4Black.setJingYiTongRequest(params);
        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("data");
        JSONArray dataListObj = jsonObject.getJSONArray("dateList");
        JSONArray jsonArray = (JSONArray) dataListObj.get(0);
        for (int j = 0; j < jsonArray.size(); j++) {
            JSONObject obj3 = jsonArray.getJSONObject(j);
            String dateStr = obj3.getString("date");
            String typeStr = obj3.getString("status");
            if (dateStr.equals("2018-09-07")) {
                if (typeStr.equals("AVAILABLE")) {
                    sb.append(dateStr + "有号了");
                }
            }
        }
        if (!"".equals(sb.toString())) {
            try {
                MailUtil.sendMail("有号了", sb.toString());
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            sb.append("no");
        }

        return sb.toString();
    }
}
